import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:bank_ocr/bank_ocr.dart';

import 'package:flutter/services.dart' show PlatformException, rootBundle;
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _ocrResultStr = '无';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await BankOcr.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('银行卡识别'),
          ),
          body: Container(
              child: Column(
            children: [
              Center(
                child: Text('Running on: $_platformVersion\n'),
              ),
              FlatButton(
                  onPressed: () async {
                    Map<String, dynamic> ocrResultMap =
                        await BankOcr.bankCardOcr;
                    print(ocrResultMap);
                    setState(() {
                      _ocrResultStr = ocrResultMap.toString();
                    });
                  },
                  child: Text("银行卡识别")),
              FlatButton(
                onPressed: () async {
                  print(await BankOcr.init);
                },
                child: Text('银行卡识别初始化'),
              ),
              FlatButton(
                onPressed: () async {
                  final imageByteData = await rootBundle.load('images/1.jpg');
                  Directory dir = await getExternalStorageDirectory();
                  String path = dir.path;
                  File imageFile = File('$path/1.jpg');
                  imageFile
                      .writeAsBytesSync(imageByteData.buffer.asInt8List(0));
                  final ocrResultMap =
                      await BankOcr.bankCardOcrFromImagePath(imageFile.path);
                  print(ocrResultMap);
                  setState(() {
                    _ocrResultStr = ocrResultMap.toString();
                  });
                },
                child: Text('银行卡图片识别'),
              ),
              Expanded(child: SingleChildScrollView(child: Text(_ocrResultStr)))
            ],
          ))),
    );
  }
}
